import 'package:ble_printer_example/printer_ticket_data.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'package:ble_printer/ble_printer.dart';
import 'package:kayo_package/kayo_package.dart';

void main() {
  runApp(Demo());
}

class Demo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyApp(),
    );
  }
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    BlePrinter.share.initState((data) {
      var map = BlePrinter.share.getMapOrList(data);
      var type = map['type'];
      if (type == EventType.BLE_FOUND) {
        var bleRetData = BleRetData<List<BleDevice>>.fromJson(map, type);
        var data = bleRetData.data;
      } else if (type == EventType.BLE_CONNECTED) {
        // IntentUtils.finish(context);
      }
      // EventType
    }, (e) {});
  }

  @override
  void dispose() {
    BlePrinter.share.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ToolBar(
      child: ListView(
        children: [
          TextView('蓝牙'),
          ButtonView(
            text: '连接蓝牙',
            onPressed: () {
              setState(() {
                // 04:7F:0E:12:4B:45
                Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (_) {
                        return KeySearchPage();
                      },
                      fullscreenDialog: false,
                    ));
              });
            },
          ),
          ButtonView(
            text: '打印',
            onPressed: () {
              setState(() {
                BlePrinter.share.bleInit(
                    success: () {
                      List<PrinterTicketData> data = [
                        PrinterTicketData(
                            company: '实打实大师打底衫 阿斯顿打算撒撒爱上爱上',
                            addressStart: '起点',
                            addressEnd: '终点',
                            carNum: '车牌',
                            driverName: '驾驶员',
                            ticketTime: '2021-10-01 11:12:19',
                            ticketUser: '开单人员',
                            ticketId: '12312312312312',
                            ticketTimes: '3',
                            ticketBarCode: 'a1231231',
                            ticketQrCode: '天府蓝'),
                        PrinterTicketData(
                            company: '实打实大师打底衫 阿斯顿打算撒撒爱上爱上',
                            addressStart: '起点2',
                            addressEnd: '终点2',
                            carNum: '车牌2',
                            driverName: '驾驶员2',
                            ticketTime: '2021-10-01 11:12:22',
                            ticketUser: '开单人员2',
                            ticketId: '12312312312312',
                            ticketTimes: '6',
                            ticketBarCode: 'a1231232',
                            ticketQrCode: '天府蓝2'),
                        PrinterTicketData(
                            company: '公司2',
                            addressStart: '起点2',
                            addressEnd: '终点2',
                            carNum: '车牌2',
                            driverName: '驾驶员2',
                            ticketTime: '2021-10-01 11:12:22',
                            ticketUser: '开单人员2',
                            ticketId: '12312312312312',
                            ticketTimes: '6',
                            ticketBarCode: 'a1231232',
                            ticketQrCode: '天府蓝2'),
                        PrinterTicketData(
                            company: '实打实大师打底衫 阿斯顿打算撒撒爱上爱上',
                            addressStart: '起点2',
                            addressEnd: '终点2',
                            carNum: '车牌2',
                            driverName: '驾驶员2',
                            ticketTime: '2021-10-01 11:12:22',
                            ticketUser: '开单人员2',
                            ticketId: '12312312312312',
                            ticketTimes: '6',
                            ticketBarCode: 'a1231232',
                            ticketQrCode: '天府蓝2'),
                        PrinterTicketData(
                            company: '公司2',
                            addressStart: '起点2',
                            addressEnd: '终点2',
                            carNum: '车牌2',
                            driverName: '驾驶员2',
                            ticketTime: '2021-10-01 11:12:22',
                            ticketUser: '开单人员2',
                            ticketId: '12312312312312',
                            ticketTimes: '6',
                            ticketBarCode: 'a1231232',
                            ticketQrCode: '天府蓝2'),
                        PrinterTicketData(
                            company: '公司2',
                            addressStart: '起点2',
                            addressEnd: '终点2',
                            carNum: '车牌2',
                            driverName: '驾驶员2',
                            ticketTime: '2021-10-01 11:12:22',
                            ticketUser: '开单人员2',
                            ticketId: '12312312312312',
                            ticketTimes: '6',
                            ticketBarCode: 'a1231232',
                            ticketQrCode: '天府蓝2')
                      ];

                      var json = BlePrinter.share.toJson(data);
                      BlePrinter.share.printData(json);
                    },
                    error: () {});
              });
            },
          ),
          ButtonView(
            text: '打印弃用',
            onPressed: () {
              setState(() {
                if (false) {
                  // BlePrinter.share.printText(left: '起点');

                  // BlePrinter.share.printQrcode('asdfakjsdfjkla;sdfjkju212io');
                  // BlePrinter.share.printTitle('成都看你好建筑工程有限公司运输单据');
                  BlePrinter.share.printText(left: '车牌号', right: '驾驶员');
                  BlePrinter.share.printText(
                      left: '川AAC119',
                      right: '驾驶驾',
                      bold: true,
                      doubleWidth: true,
                      doubleHeight: true);

                  return;
                }
                for (var i in [
                  1 /*, 2, 3, 4, 5, 6, 7, 8, 9, 10*/
                ]) {
                  BlePrinter.share.printTitle('成都看你好建筑工程有限公司运输单据');
                  BlePrinter.share.printEmptyLine();

                  BlePrinter.share.printText(left: '起点');
                  BlePrinter.share.printDashLine();
                  BlePrinter.share.printText(
                      left: '对佛i啊的房间阿莱克斯的烦恼卡就开始的饭',
                      bold: true,
                      doubleHeight: true,
                      doubleWidth: true);
                  BlePrinter.share.printEmptyLine();

                  BlePrinter.share.printText(left: '车牌号', right: '驾驶员');
                  BlePrinter.share.printDashLine();
                  BlePrinter.share.printText(
                      left: '川AAC119',
                      right: '驾驶',
                      bold: true,
                      doubleWidth: true,
                      doubleHeight: true);
                  BlePrinter.share.printEmptyLine();

                  BlePrinter.share.printText(left: '开单时间');
                  BlePrinter.share.printDashLine();
                  BlePrinter.share
                      .printText(left: '2020-10-10 12:12:13', bold: true);
                  BlePrinter.share.printEmptyLine();

                  BlePrinter.share.printText(left: '开单人');
                  BlePrinter.share.printDashLine();
                  BlePrinter.share.printText(left: '李小猫', bold: true);
                  BlePrinter.share.printEmptyLine();

                  BlePrinter.share.printText(left: '终点');
                  BlePrinter.share.printDashLine();
                  BlePrinter.share.printText(left: '打了卡上的饭看了卡口老地方', bold: true);
                  BlePrinter.share.printEmptyLine();

                  BlePrinter.share.printText(left: '运单号');
                  BlePrinter.share.printDashLine();
                  BlePrinter.share
                      .printText(left: '123123124324123413241234', bold: true);
                  BlePrinter.share.printEmptyLine();

                  BlePrinter.share.printText(left: '当日趟次');
                  BlePrinter.share.printDashLine();
                  BlePrinter.share.printText(left: '10', bold: true);
                  BlePrinter.share.printEmptyLine(lines: 1);

                  BlePrinter.share
                      .printBarcode('af121323', height: 90, width: 1);
                  BlePrinter.share.printEmptyLine(lines: 2);
                  BlePrinter.share.printQrcode('asdfakjsdfjkla;sdfjkju212io');
                  BlePrinter.share.printEmptyLine(lines: 2);
                  BlePrinter.share.printDashLine();
                }
              });
            },
          ),
        ],
      ),
    );
  }
}
